<?php
/*****************************************************
 * File name: Version.php
 * Create date: 2019/03/28
 * Author: liuyue
 * Description: 版本模块cgi
 *****************************************************/

namespace Ctrl;

use Model;

use Lib;
use Lib\Paas\Login;
use Lib\Service\JsonService;

class Version extends Lib\Base\Ctrl
{

    private $db;
    private $savePath;



    public function __construct()
    {
        //校验管理员权限
        $roleService = new Lib\Paas\Role();
        $checkAdmin = $roleService->getRoleInfo();
//        if ($checkAdmin['role'] != ROLE_ID) {
//            die('抱歉！您没有访问权限');
//        }
//        if (!in_array(MODULE_ID, $checkAdmin['moduleID']) ) {
//            die('抱歉！您没有访问权限');
//        }
        $this->db = Lib\Db\DbModel::init(); // 构造方法，线上自动切换
        //环境判断
        if (ENV == 'test' && ENVIRONMENT == 'NON_OA') {
            $this->savePath = APP . 'upload/';
        } else {
            $this->savePath = UPLOADED_PATH;
        }
    }

    public function index()
    {
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    /**
     * 切换数据版本
     */
    public function switchVersion()
    {
        session_start();

        //实例化Login类
        $login = new Login();

        if (IS_POST) {
            //清空session
            unset($_SESSION['heroId']);
            unset($_SESSION['skinId']);
            if(empty(I('post.id')) || empty(I('post.versionNumber'))){
                $this->ajaxError('数据为空');
            }
            $versionInfo = array(
                'id' => I('post.id'),
                'versionNumber  ' => I('post.versionNumber'),
                'isCurrent' => 1,
                'updateTime' => time(),
            );
            $model = new Model\VersionModel();
            if ($model->editVersion($versionInfo)) {
                //切换时,当前版本所有的英雄皮肤及道具需要重新发布
                $res = $this->db->table('tbversion')->where(['isCurrent' => 1])->find('id');
                $where = [
                    'versionId' => $res['id']
                ];
                $data = [
                    'releasestatus' => 0,
                    'updateMan' => $login->getLoginInfo()['name'],
                    'updateTime' => date("Y-m-d H:i:s",time())
                ];
                //事务
                try {
                    $this->db->begin();
                    //更新英雄表
                    $this->db->table('tbhero')->where($where)->update($data);
                    //更新皮肤表
                    $this->db->table('tbskins')->where($where)->update($data);
                    //更新道具表
                    $this->db->table('tbitems')->where($where)->update($data);
                    $this->db->commit();
                    $this->ajaxSuccess("切换成功");
                } catch (\Exception $e) {
                    $this->db->rollBack();
                    $this->ajaxError('切换失败');
                }
            } else {
                $this->ajaxError('切换失败');
            }
        } else {
            //清空session
            unset($_SESSION['heroId']);
            unset($_SESSION['skinId']);
            //获取版本号
            $versionInfo = $this->db->table('tbversion')->where(['isCurrent' => 1])->find();
            $this->assign('id', $_GET['id']);
            $this->assign('versionInfo', $versionInfo['versionNumber']);
            $this->display("Admin/Version/switchVersion");
        }
    }

    /**
     * 检查版本
     */
    public function checkVersion()
    {
        if (IS_POST) {
            //添加输入的版本
            $versionNum = I('version_number');
            if (empty($versionNum)) {
                $res['code'] = 1;
                $res['msg'] = '版本号不能为空';
                $this->ajaxReturn($res);
            }
            $model = new Model\VersionModel();
            $result = $model->isExistsVersion($versionNum);
            if ($result) {
                $res['code'] = 1;
                $res['msg'] = '版本已存在';
                $res['versionNum'] = $versionNum;
                $this->ajaxReturn($res);
            } else {
                $res['code'] = 0;
                $res['msg'] = '这是新版本';
                $res['versionNum'] = $versionNum;
                $this->ajaxReturn($res);
            }
        }
    }

    /**
     * 复制版本
     */
    public function copyVersion()
    {
        if(IS_POST){
            //添加输入的版本
            $versionNum = I('versionNumber');
            $model = new Model\VersionModel();
            //校验版本号
            $check = $model->pregVersion($versionNum);
            if($check === false){
                $res['code'] = 1;
                $res['msg'] = "不是一个正确的版本号";
                return $res;
            }
            if(empty($versionNum)){
                $res['code'] = 1;
                $res['msg'] = '版本号不能为空';
                $this->ajaxReturn($res);
            }
            $result = $model->isExistsVersion($versionNum);
            if ($result) {
                $res['code'] = 1;
                $res['msg'] = '版本已存在';
                $res['versionNum'] = $versionNum;
                $this->ajaxReturn($res);
            } else {
                //实例化Login类
                $login = new Login();

                $save['versionNumber'] = $versionNum;
                $save['createTime'] = date('Y-m-d H:i:s', time());
                $save['creater'] = $login->getLoginInfo()['name'];

                //查询出当前版本所有的数据
                $model = new Model\VersionModel();
                $currentVersion = $model->getCurrentVersion();
                $where = [
                    'versionId'=> $currentVersion['id']
                ];
                $createName = $login->getLoginInfo()['name'];
                $time = date('Y-m-d H:i:s', time());
                //开启事务
                try {
                    $this->db->begin();
                    $flag = 1;
                    $e['code'] = 404;
                    $e['msg'] = '复制版本失败，请查看日志';
                    //复制版本
                    $res = $this->db->table('tbversion')->insert($save);
                    logWrite(1, $res, 'tbversion');
                    if($res ===false){
                        recordAMSLog(__FILE__ . "," . __LINE__ . ",复制".$versionNum."版本失败.");
                        $this->db->rollback();
                        logWrite(5, false, '', ",复制".$versionNum."版本失败.");
                        $this->ajaxReturn($e);
                        exit;
                    }
                    //查询英雄表
                    $allHero = $this->db->table('tbhero')->where($where)->select();
                    foreach ($allHero as $key=>$val){
                        //清空数组里面的值
                        unset($allHero[$key]['id']);
                        unset($allHero[$key]['updateMan']);
                        unset($allHero[$key]['updateTime']);
                        $allHero[$key]['creater'] = $createName;
                        $allHero[$key]['createTime'] = $time;
                        $allHero[$key]['versionId'] = intval($res);
                    }
                    //英雄
                    $flag = $this->db->table('tbhero')->insertAll($allHero);
                    if($flag===false){
                        recordAMSLog(__FILE__ . "," . __LINE__ . ",复制".$versionNum."版本失败.");
                        $this->db->rollback();
                        logWrite(5, false, '', ",复制".$versionNum."版本失败.");
                        $this->ajaxReturn($e);
                        exit;
                    }
                    //查询道具表
                    $allItems =$this->db->table('tbitems')->where($where)->select();
                    foreach ($allItems as $key=>$val){
                        //清空数组里面的值
                        unset($allItems[$key]['id']);
                        unset($allItems[$key]['updateMan']);
                        unset($allItems[$key]['updateTime']);
                        $allItems[$key]['creater'] = $createName;
                        $allItems[$key]['createTime'] = $time;
                        $allItems[$key]['versionId'] = intval($res);
                    }
                    //道具
                    $flag = $this->db->table('tbitems')->insertAll($allItems);
                    if($flag===false){
                        recordAMSLog(__FILE__ . "," . __LINE__ . ",复制".$versionNum."版本失败.");
                        $this->db->rollback();
                        logWrite(5, false, '', ",复制".$versionNum."版本失败.");
                        $this->ajaxReturn($e);
                        exit;
                    }
                    //查询皮肤表
                    $allSkins = $this->db->table('tbskins')->where($where)->select();
                    foreach ($allSkins as $key=>$val){
                        //清空数组里面的值
                        unset($allSkins[$key]['id']);
                        unset($allSkins[$key]['updateMan']);
                        unset($allSkins[$key]['updateTime']);
                        $allSkins[$key]['creater'] = $createName;
                        $allSkins[$key]['createTime'] = $time;
                        $allSkins[$key]['versionId'] = intval($res);
                    }
                    //皮肤
                    $flag = $this->db->table('tbskins')->insertAll($allSkins);
                    if($flag===false){
                        recordAMSLog(__FILE__ . "," . __LINE__ . ",复制".$versionNum."版本失败.");
                        $this->db->rollback();
                        logWrite(5, false, '', ",复制".$versionNum."版本失败.");
                        $this->ajaxReturn($e);
                        exit;
                    }
                    //查询技能表
                    $allSpells = $this->db->table('tbspells')->where($where)->select();
                    foreach ($allSpells as $key=>$val){
                        //清空数组里面的值
                        unset($allSpells[$key]['id']);
                        unset($allSpells[$key]['updateMan']);
                        unset($allSpells[$key]['updateTime']);
                        $allSpells[$key]['creater'] = $createName;
                        $allSpells[$key]['createTime'] = $time;
                        $allSpells[$key]['versionId'] = intval($res);
                    }
                    //技能
                    $flag = $this->db->table('tbspells')->insertAll($allSpells);
                    if($flag===false){
                        recordAMSLog(__FILE__ . "," . __LINE__ . ",复制".$versionNum."版本失败.");
                        $this->db->rollback();
                        logWrite(5, false, '', ",复制".$versionNum."版本失败.");
                        $this->ajaxReturn($e);
                        exit;
                    }
                    if ($flag !==false) {
                        $this->db->commit();
                        $e['code'] = 0;
                        $e['msg'] = '复制版本成功';
                        $e['versionNum'] = $versionNum;
                        recordAMSLog(__FILE__ . "," . __LINE__ .  ",复制".$versionNum."版本成功.");
                        logWrite(5, true, '',  ",复制".$versionNum."版本成功.");
                        $this->ajaxReturn($e);
                        exit;
                    }
                } catch (\Exception $e) {
                    recordAMSLog(__FILE__ . "," . __LINE__ . ",复制".$versionNum."版本失败.");
                    $this->db->rollback();
                    logWrite(5, false, '', ",复制".$versionNum."版本失败.");
                    $this->ajaxReturn($e);
                }
            }
        }
    }

    /**
     * 导入Json文件
     */
    public function insertJson()
    {

        ini_set('memory_limit', -1);
        ini_set('max_execution_time', 0);

        if (IS_POST) {
            //输入的版本
            $versionNum = I('versionNum');
            if (empty($versionNum)) {
                $res['code'] = 1;
                $res['msg'] = '版本号不能为空';
                $this->ajaxReturn($res);
            }
            $model = new Model\VersionModel();
            //校验版本号
            $check = $model->pregVersion($versionNum);
            if($check === false){
                $res['code'] = 1;
                $res['msg'] = '版本号不符合规则';
                $this->ajaxReturn($res);
            }
            //返回版本号id
            $versionId = $model->insertVersionReturnId($versionNum);
            if (empty($versionId)) {
                $res['code'] = 1;
                $res['msg'] = '创建版本失败';
                $this->ajaxReturn($res);
            }
            //获取文件目录
            $json_dir = I('post.json_dir');

            $path = $this->savePath . $json_dir;

            //获取文件
            $file = read_all_file($path);

            //开启事务
            $this->db->begin();

            $falg = 0;
            $e['code'] = 404;
            $e['msg'] = '导入失败，请查看日志';

            foreach ($file as $k => $v) {
                $service = new JsonService(1, $v, $versionId);

                $data = $service->getDataByFile();
                if (empty($data)) {
                    logWrite(6, false, '', $v . "获取不到文件内容");
                    recordAMSLog(__FILE__ . "," . __LINE__ . ",获取不到文件内容：" . $v);
                    continue;
                }
                if ($data['code'] !== 0) {
                    logWrite(6, false, '',
                        "path:" . $v . ",msg:" . $data['msg'] . ",type:" . $data['type'] . ",code:" . $data['code']);
                    recordAMSLog(__FILE__ . "," . __LINE__ . ",code 错误：" . $v . ",msg:" . $data['msg'] . ",type:" . $data['type'] . ",code:" . $data['code']);
                    continue;
                }
                switch ($data['type']) {
                    case $service::CATE_TMOBA_CHAMPION://英雄tmoba数据
                        //更新英雄
                        $falg = $this->updateHero($versionId, $data['data']);
                        if ($falg === 1) {
                            recordAMSLog(__FILE__ . "," . __LINE__ . ",英雄导入失败：" . $v);
                            $this->db->rollback();
                            logWrite(6, false, '', "英雄导入失败：" . $v);
                            $this->ajaxReturn($e);
                            exit;
                        }
                        //更新皮肤数据
                        $falg = $this->updateSkins($versionId, $data['skins']);
                        if ($falg === 1) {
                            recordAMSLog(__FILE__ . "," . __LINE__ . ",英雄皮肤导入失败：" . $v);
                            $this->db->rollback();
                            logWrite(6, false, '', "英雄皮肤导入失败：" . $v);
                            $this->ajaxReturn($e);
                            exit;
                        }
                        //更新技能数据
                        $falg = $this->updateSpells($versionId, $data['spells']);
                        if ($falg === 1) {
                            recordAMSLog(__FILE__ . "," . __LINE__ . ",英雄技能导入失败：" . $v);
                            $this->db->rollback();
                            logWrite(6, false, '', "英雄技能导入失败：" . $v);
                            $this->ajaxReturn($e);
                            exit;
                        }
                        break;
                    case $service::CATE_JSON_CHAMPION://英雄json数据
                        //更新英雄
                        $falg = $this->updateHero($versionId, $data['data']);
                        if ($falg === 1) {
                            recordAMSLog(__FILE__ . "," . __LINE__ . ",英雄导入失败：" . $v);
                            $this->db->rollback();
                            logWrite(6, false, '', "英雄导入失败：" . $v);
                            $this->ajaxReturn($e);
                            exit;
                        }
                        //更新技能数据
                        $falg = $this->updateSpells($versionId, $data['spells']);
                        if ($falg === 1) {
                            recordAMSLog(__FILE__ . "," . __LINE__ . ",英雄技能导入失败：" . $v);
                            $this->db->rollback();
                            logWrite(6, false, '', "英雄技能导入失败：" . $v);
                            $this->ajaxReturn($e);
                            exit;
                        }
                        break;
                    case $service::CATE_TMOBA_ITEM://道具tmoba数据
                    case $service::CATE_JSON_ITEM://道具json数据
                        //更新道具数据
                        $falg = $this->updateItem($versionId, $data['data']);
                        if ($falg === 1) {
                            recordAMSLog(__FILE__ . "," . __LINE__ . ",道具数据导入失败：" . $v);
                            $this->db->rollback();
                            logWrite(6, false, '', "道具数据导入失败：" . $v);
                            $this->ajaxReturn($e);
                            exit;
                        }

                        //更新道具合成等级 太耗时放脚本里面完成
//                        $falg = $this->updateItemComposeLevel($versionId, $data['data']);
//                        if ($falg === 1) {
//                            recordAMSLog(__FILE__ . "," . __LINE__ . ",更新道具数据合成等级失败：" . $v);
//                            $db->rollback();
//                            $this->ajaxReturn($e);
//                            exit;
//                        }

                        break;
                    case $service::CATE_TMOBA_SKINS://皮肤tmoba数据
                        continue;
                        break;
                    default:
                        recordAMSLog(__FILE__ . "," . __LINE__ . ",不支持的文件：" . $v);
                        logWrite(6, false, '', "不支持的文件：" . $v);
                        continue;
                        break;

                }
                if ($falg === 0) {
                    //记录导入日记
                    logWrite(6, true, '', '文件：' . $v . '导入成功');
                }

            }
            //皮肤最后处理 否者先导入皮肤tmoba数据 再导入英雄数据 会造成 皮肤表英雄ID为0
            foreach ($file as $k => $v) {
                $service = new JsonService(1, $v, $versionId);

                $data = $service->getDataByFile();
                if (empty($data)) {
                    continue;
                }
                if ($data['code'] !== 0) {
                    continue;
                }
                switch ($data['type']) {
                    case $service::CATE_TMOBA_SKINS://皮肤tmoba数据
                        //更新皮肤数据
                        $falg = $this->updateSkins($versionId, $data['data']);
                        if ($falg === 1) {
                            recordAMSLog(__FILE__ . "," . __LINE__ . ",皮肤tmoba数据导入失败：" . $v);
                            $this->db->rollback();
                            logWrite(6, false, '', "皮肤tmoba数据导入失败：" . $v);
                            $this->ajaxReturn($e);
                            exit;
                        }
                        //更新炫彩皮肤
                        $falg = $this->updateSkins($versionId, $data['chromas']);
                        if ($falg === 1) {
                            recordAMSLog(__FILE__ . "," . __LINE__ . ",炫彩皮肤tmoba数据导入失败：" . $v);
                            $this->db->rollback();
                            logWrite(6, false, '', "炫彩皮肤tmoba数据导入失败：" . $v);
                            $this->ajaxReturn($e);
                            exit;
                        }
                        //更新炫彩皮肤的英雄数据
                        $falg = $this->updateSkinsHeroData($versionId, $data['chromas']);
                        if ($falg === 1) {
                            recordAMSLog(__FILE__ . "," . __LINE__ . ",更新炫彩皮肤的英雄数据失败：" . $v);
                            $this->db->rollback();
                            logWrite(6, false, '', "更新炫彩皮肤的英雄数据失败：" . $v);
                            $this->ajaxReturn($e);
                            exit;
                        }
                        break;
                    default:
                        continue;
                        break;
                }
                if ($falg === 0) {
                    //记录导入日记
                    logWrite(6, true, '', '文件：' . $v . '导入成功');
                }

            }
            if ($falg === 0) {
                $this->db->commit();
                $e['code'] = 0;
                $e['msg'] = '操作成功';
                recordAMSLog(__FILE__ . "," . __LINE__ . '版本：' . $versionNum . '导入成功');
                logWrite(6, true, '', '版本：' . $versionNum . '导入成功');
                $this->ajaxReturn($e);
            }
        }
    }

    /**
     * 导入道具
     * @param $versionId
     * @param $item
     * @return int
     */
    private function updateItem($versionId, $item)
    {
        if (!is_array($item)) {
            recordAMSLog(__FILE__ . "," . __LINE__ . ",无要添加的数据" . var_export($item, true));
            return 0;
        }
        //检测是否可以批量添加
        $count = $this->db->table('tbitems')->where(['versionId' => $versionId])->count();
        $falg = 0;
        $user = new Lib\Paas\Login();
        $userInfo = $user->getLoginInfo();
        if ($count > 0) {
            $where['versionId'] = $versionId;
            //一条一条更新
            foreach ($item as $k => $v) {
                $where['itemId'] = $v['itemId'];
                $find = $this->db->table('tbitems')->where($where)->find('id');

                $save_data = $v;
                $save_data['versionId'] = $versionId;

                if ($find) {
                    $save_data['updateTime'] = date('Y-m-d H:i:s', time());
                    $save_data['updateMan'] = $userInfo['name'];
                    //有数据 更新
                    $res = $this->db->table('tbitems')->where($where)->update($save_data);
                    if ($res === false) {
                        $falg = 1;
                        recordAMSLog(__FILE__ . "," . __LINE__ . '更新道具：' . $save_data['itemId'] . '失败');
                        return $falg;
                    }
                } else {
                    $save_data['createTime'] = date('Y-m-d H:i:s', time());
                    $save_data['creater'] = $userInfo['name'];
                    //无历史数据 新增
                    $res = $this->db->table('tbitems')->insert($save_data);
                    if ($res === false) {
                        $falg = 1;
                        recordAMSLog(__FILE__ . "," . __LINE__ . '新增道具：' . $save_data['itemId'] . '失败');
                        return $falg;
                    }
                }
            }
        } else {
            foreach ($item as $k => $v) {
                $item[$k]['versionId'] = $versionId;
                $item[$k]['createTime'] = date('Y-m-d H:i:s', time());
                $item[$k]['creater'] = $userInfo['name'];
            }

            //批量操作
            $res = $this->db->table('tbitems')->insertAll($item);
            if ($res === false) {
                $falg = 1;
                recordAMSLog(__FILE__ . "," . __LINE__ . '批量新增道具失败');
                return $falg;
            }
        }
        logWrite(6, true, 'tbitems', '道具导入成功');
        recordAMSLog(__FILE__ . "," . __LINE__ . '道具导入成功');
        return $falg;

    }

    /**
     * 更新道具合成等级
     * @param $versionId
     * @param $item
     * @return int
     */
    private function updateItemComposeLevel($versionId, $item)
    {
        $falg = 0;
        $user = new Lib\Paas\Login();
        $userInfo = $user->getLoginInfo();

        foreach ($item as $k => $v) {
            $where['itemId'] = $v['itemId'];
            $where['versionId'] = $versionId;
            $save_data['updateTime'] = date('Y-m-d H:i:s', time());
            $save_data['updateMan'] = $userInfo['name'];
            $save_data['composeLevel'] = getItemLevelNum($v['itemId'],$versionId);
            //有数据 更新
            $res = $this->db->table('tbitems')->where($where)->update($save_data);
            if ($res === false) {
                $falg = 1;
                recordAMSLog(__FILE__ . "," . __LINE__ . '更新道具合成等级：' . $v['itemId'] . '失败');
                return $falg;
            }
        }
        logWrite(6, true, 'tbitems', '更新道具合成等级成功');
        recordAMSLog(__FILE__ . "," . __LINE__ . '更新道具合成等级成功');
        return $falg;
    }

    /**
     * 更新英雄数据
     * @param $versionId
     * @param $hero
     * @return int
     */
    private function updateHero($versionId, $hero)
    {
        if (!is_array($hero)) {
            recordAMSLog(__FILE__ . "," . __LINE__ . ",无要添加的数据" . var_export($hero, true));
            return 0;
        }
        //检测是否可以批量添加
        $falg = 0;
        $where['versionId'] = $versionId;
        $where['heroId'] = $hero['heroId'];
        $find = $this->db->table('tbhero')->where($where)->find();
        $save_data = $hero;
        $user = new Lib\Paas\Login();
        $userInfo = $user->getLoginInfo();
        $save_data['versionId'] = $versionId;

        if ($find) {
            $save_data['updateTime'] = date('Y-m-d H:i:s', time());
            $save_data['updateMan'] = $userInfo['name'];
            //有数据 更新
            $res = $this->db->table('tbhero')->where($where)->update($save_data);

            if ($res === false) {
                $falg = 1;
                recordAMSLog(__FILE__ . "," . __LINE__ . '更新英雄：' . $save_data['heroId'] . '失败');
                return $falg;
            }
        } else {
            $save_data['createTime'] = date('Y-m-d H:i:s', time());
            $save_data['creater'] = $userInfo['name'];
            //无历史数据 新增
            $res = $this->db->table('tbhero')->insert($save_data);
            if ($res === false) {
                $falg = 1;
                recordAMSLog(__FILE__ . "," . __LINE__ . '更新英雄：' . $save_data['heroId'] . '失败');
                return $falg;
            }
        }
        logWrite(6, true, 'tbhero', '英雄导入成功');
        recordAMSLog(__FILE__ . "," . __LINE__ . '英雄导入成功');
        return $falg;

    }

    /**
     * 更新皮肤
     * @param $versionId
     * @param $skins
     * @return int
     */
    private function updateSkins($versionId, $skins)
    {
        if (!is_array($skins)) {
            recordAMSLog(__FILE__ . "," . __LINE__ . ",无要添加的数据" . var_export($skins, true));
            return 0;
        }
        //检测是否可以批量添加
        $count = $this->db->table('tbskins')->where(['versionId' => $versionId])->count();
        $falg = 0;
        $user = new Lib\Paas\Login();
        $userInfo = $user->getLoginInfo();
        if ($count > 0) {
            $where['versionId'] = $versionId;
            //一条一条更新
            foreach ($skins as $k => $v) {
                $where['skinId'] = $v['skinId'];
                $find = $this->db->table('tbskins')->where($where)->find('id');

                $save_data = $v;
                $save_data['versionId'] = $versionId;

                if ($find) {
                    $save_data['updateTime'] = date('Y-m-d H:i:s', time());
                    $save_data['updateMan'] = $userInfo['name'];
                    //有数据 更新
                    $res = $this->db->table('tbskins')->where($where)->update($save_data);

                    if ($res === false) {
                        $falg = 1;
                        recordAMSLog(__FILE__ . "," . __LINE__ . '更新皮肤：' . $save_data['skinId'] . '失败');
                        return $falg;
                    }
                } else {
                    $save_data['createTime'] = date('Y-m-d H:i:s', time());
                    $save_data['creater'] = $userInfo['name'];
                    //无历史数据 新增
                    $res = $this->db->table('tbskins')->insert($save_data);

                    if ($res === false) {
                        $falg = 1;
                        recordAMSLog(__FILE__ . "," . __LINE__ . '新增皮肤：' . $save_data['skinId'] . '失败');
                        return $falg;
                    }
                }
            }
        } else {
            foreach ($skins as $k => $v) {
                $skins[$k]['versionId'] = $versionId;
                $skins[$k]['createTime'] = date('Y-m-d H:i:s', time());
                $skins[$k]['creater'] = $userInfo['name'];
            }
            //批量操作
            $res = $this->db->table('tbskins')->insertAll($skins);

            if ($res === false) {
                $falg = 1;
                recordAMSLog(__FILE__ . "," . __LINE__ . '批量新增皮肤失败');
                return $falg;
            }
        }
        logWrite(6, true, 'tbskins', '皮肤导入成功');
        recordAMSLog(__FILE__ . "," . __LINE__ . '皮肤导入成功');
        return $falg;

    }

    /**
     * 更新英雄炫彩皮肤
     * @param $versionId
     * @param $skins
     * @return int
     */
    private function updateSkinsHeroData($versionId, $skins)
    {
        if (!is_array($skins)) {
            recordAMSLog(__FILE__ . "," . __LINE__ . ",无要添加的数据" . var_export($skins, true));
            return 0;
        }
        //检测是否可以批量添加
        $db = Lib\Db\DbModel::init(); // 构造方法，线上自动切换
        $falg = 0;
        $user = new Lib\Paas\Login();
        $userInfo = $user->getLoginInfo();
        $where['versionId'] = $versionId;
        $save_where['versionId'] = $versionId;
        //一条一条更新
        foreach ($skins as $k => $v) {
            $where['skinId'] = $v['chromasBelongId'];
            $save_where['skinId'] = $v['skinId'];
            $find = $this->db->table('tbskins')->where($where)->find('heroId,heroName,heroTitle');

            $save_data = $v;

            if ($find) {
                $save_data['updateTime'] = date('Y-m-d H:i:s', time());
                $save_data['updateMan'] = $userInfo['name'];
                $save_data['heroId'] = $find['heroId'];
                $save_data['heroName'] = $find['heroName'];
                $save_data['heroTitle'] = $find['heroTitle'];
                $save_data['loadingImg'] = $this->chromasSkinLoadingImg.$find['heroId'].'/'.$v['skinId'].'.png';
                //有数据 更新
                $res = $this->db->table('tbskins')->where($save_where)->update($save_data);

                if ($res === false) {
                    $falg = 1;
                    recordAMSLog(__FILE__ . "," . __LINE__ . '更新炫彩皮肤：' . $save_data['skinId'] . '失败');
                    return $falg;
                }
            } else {
                logWrite(6, false, 'tbskins', '找不到炫彩皮肤的主皮肤：' . $save_data['skinId']);
                recordAMSLog(__FILE__ . "," . __LINE__ . '找不到炫彩皮肤的主皮肤：' . $save_data['skinId'] . '失败');
            }
        }
        logWrite(6, true, 'tbskins', '更新炫彩皮肤成功');
        recordAMSLog(__FILE__ . "," . __LINE__ . '更新炫彩皮肤成功');
        return $falg;

    }

    /**
     * 更新技能
     * @param $versionId
     * @param $spells
     * @return int
     */
    private function updateSpells($versionId, $spells)
    {
        if (!is_array($spells)) {
            recordAMSLog(__FILE__ . "," . __LINE__ . ",无要添加的数据" . var_export($spells, true));
            return 0;
        }
        $falg = 0;
        $where['versionId'] = $versionId;
        $user = new Lib\Paas\Login();
        $userInfo = $user->getLoginInfo();
        //一条一条更新
        foreach ($spells as $k => $v) {
            $where['heroId'] = $v['heroId'];
            $where['name'] = $v['name'];
            $find = $this->db->table('tbspells')->where($where)->find('id');
            $save_data = $v;
            $save_data['versionId'] = $versionId;

            if ($find) {
                $save_data['updateTime'] = date('Y-m-d H:i:s', time());
                $save_data['updateMan'] = $userInfo['name'];
                //有数据 更新
                $res = $this->db->table('tbspells')->where($where)->update($save_data);
                if ($res === false) {
                    $falg = 1;
                    recordAMSLog(__FILE__ . "," . __LINE__ . '更新技能：' . $save_data['spellKey'] . '失败');
                    return $falg;
                }
            } else {
                $save_data['createTime'] = date('Y-m-d H:i:s', time());
                $save_data['creater'] = $userInfo['name'];
                //无历史数据 新增
                $res = $this->db->table('tbspells')->insert($save_data);
                if ($res === false) {
                    $falg = 1;
                    recordAMSLog(__FILE__ . "," . __LINE__ . '新增技能：' . $save_data['spellKey'] . '失败');
                    return $falg;
                }
            }
        }
        logWrite(6, true, 'tbspells', '英雄技能导入成功');
        recordAMSLog(__FILE__ . "," . __LINE__ . '英雄技能导入成功');
        return $falg;

    }
}